<?php
/**********************************************************************//**\file
	Fetch Armory Homepage

	Description:  Displays a list of all records in the db and provides some
		sorting options
*******************************************************************************/


	require_once('core/core.php');
	
	# Initialize objects
	$mysqli = new mysqli($config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname']);
	$page = new amorypage();

	$page->title($config['guildname'].' Guild Roster');
	$page->add('<h1>'.$config['guildname'].' Guild Roster</h1>');
	
	$page->add('<div id="roster">');

	#Sort by Default / Name / Level
	$sort = 'profile_level DESC, profile_rank, profile_name'; #default
	$urlname = 'name'; #default url
	$urllevel = 'level'; #default url
	$button = '';
	if(isset($_GET['sort']))
	{
		switch ($_GET['sort'])
		{
			case 'name':
				$sort = 'profile_name';
				$urlname = 'namei'; #enables sort toggle
				break;
			case 'namei':
				$sort = 'profile_name DESC';
				break;
			case 'level':
				$sort = 'profile_level DESC, profile_name';
				$urllevel = 'leveli'; #enables sort toggle
				break;
			case 'leveli':
				$sort = 'profile_level, profile_name';
				break;
		}
		$button = '<span style="position: relative; float: left; margin: 2px -21px -2px 3px;"><a href="."><img src="/theme/cancel.png" alt="Cancel Icon" title="Clear sorting options" /></a></span>';
	}

	#Search for Role / Trade
	$page->add('<table><thead><tr><th>'.$button.'<a href="?sort='.$urlname.'">Name</a></th><th><a href="?sort='.$urllevel.'">Level</a></th><th>Race</th><th>Class</th><th colspan="4">Talents</th><th colspan="4">Professions</th></tr></thead>');

	$expired = time() - 276480;
	$query = "SELECT profile_name, profile_level, profile_race, profile_gender, profile_class, profile_talents1, profile_talents2, profile_talents3, profile_profname1, profile_profskill1, profile_profname2, profile_profskill2, profile_url, profile_rank FROM profile_roster WHERE profile_updated > $expired ORDER BY $sort;";

	if($result = $mysqli->query($query))
	{
		$rowalt = true;
		while($row = $result->fetch_row())
		{
			# Initialize localazation object
			$t = new armorytext();

			$page->add('<tr');
			if($rowalt)
				$page->add(' class=alt');
			$page->add('><td class="name"><a style="float: left;" href="http://www.wowarmory.com/character-sheet.xml?'.$row[12].'">'.utf8_encode($row[0]).'</a>');
			if($row[13] == '1' && $row[1] >= 70)
				$page->add('<img src="icons/rank/officer.jpg" title="Officer" style="float: right;" />');
			if($row[13] == '0')
				$page->add('<img src="icons/rank/gm.jpg" title="Guild Master" style="float: right;" />');
			$page->add('</td><td>'.$row[1].'</td>');

			# Gender / race / class
			$page->add('<td><img src="icons/race/'.$row[2].'-'.$row[3].'.gif" /></td><td><img src="icons/class/'.$row[4].'.gif" /></td>');

			# Talents
			$page->add("<td class=\"t1\">$row[5]</td><td class=\"t2\">$row[6]</td><td class=\"t3\">$row[7]</td>");

			# Talent Summary (prot, for instance)
			$page->add('<td><a href="http://www.wowarmory.com/character-talents.xml?'.$row[12].'">');
			$tt = ($row[1]-9)*.65; # Hybrid threshold at <65% total talents invested in one tree.
			if($row[5] < $tt && $row[6] < $tt && $row[7] < $tt)
				$page->add('Hybrid');
			else
			{
				if($row[5] > $row[6] && $row[5] > $row[7])
					$page->add($t->t($row[4], 0));
				elseif ($row[6] > $row[7])
					$page->add($t->t($row[4], 1));
				else
					$page->add($t->t($row[4], 2));
			}
			$page->add('</a></td>');

			# Professions
			if($row[8] != '')
				$page->add('<td class="prof"><img src="icons/prof/'.$row[8].'.jpg" title="'.$row[8].'" /></td><td>'.$row[9].'</td>');
			else
				$page->add('<td class="prof"></td><td></td>');
			if($row[10] != '')
				$page->add('<td><img src="icons/prof/'.$row[10].'.jpg" title="'.$row[10].'" /></td><td>'.$row[11].'</td>');
			else
				$page->add('<td></td><td></td>');

			$page->add('</tr>');
			$rowalt=!$rowalt;
		}
		$numprofiles = $mysqli->affected_rows;
		$result->close();
	}
	else
	{
		
	}

	$page->add('</table>');

	#Output summary
	/*$result = $mysqli->query('SELECT profile_updated FROM profile_roster ORDER BY profile_updated DESC LIMIT 1;');
	$row = $result->fetch_row();
	$date = date('Y-m-d @ H:i', $row[0]);
	$page->add("<p>Currently displaying data from $numprofiles profiles. Last updated on $date.</p>");
	$page->add('<p>Powered by <a href="http://code.google.com/p/fetch-armory/">Fetch Armory</a>.</p>');
	$result->close();*/

	$mysqli->close();
	
	$page->add('</div>');

	$page->output();
?>
